<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input size="mini" v-model="listQuery.userName" clearable class="filter-item" style="width: 200px;" placeholder="评论人名字"/>
      <el-button
        size="mini"
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter">查找
      </el-button>
      <el-button size="mini" class="filter-item" type="success" icon="el-icon-edit" @click="batchPartnerUpdate">批量审核消息</el-button>
      <!-- <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button> -->
    </div>
    <!-- 类型-->
    <div class="filter-container">
      <el-tag
        v-for="(tag,index) in tags"
        :key="tag.name"
        :class="bgindex === index?'active':''"
        style="cursor: pointer;margin-right: 10px"

        @click.native="tagclick(tag.cid,index)">
        {{ tag.name }}
      </el-tag>
    </div>
    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      size="small"
      border
      element-loading-text="正在查询中。。。"
      highlight-current-row
      @selection-change="handleBatchPartnerChange"
    >
      <el-table-column
        type="selection"
        width="45"
        label="选择"/>
      <el-table-column align="center" label="评论人名字" prop="userName" width="150px"/>
      <el-table-column align="center" label="星级" prop="starLevel" width="150px"/>

      <!--<el-table-column align="center" label="内容" prop="content"/>-->
      <!-- <el-table-column align="center" label="内容" prop="content">
        <template slot-scope="scope">
          <el-popover placement="right"  trigger="hover">
            <span slot="reference">详情</span>
            <span>{{ scope.row.content }}</span>
          </el-popover>
        </template>
      </el-table-column> -->

      <el-table-column align="center" label="状态" prop="deleteMark" width="150px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.deleteMark===0" type="success">正常</el-tag>
          <el-tag v-if="scope.row.deleteMark===1" type="info">隐藏</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="添加时间" width="150px" prop="createDate"/>

      <el-table-column align="center" label="操作" width="200px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.deleteMark===0" type="info" size="mini" @click="handleStatus(scope.row,1)">隐藏</el-button>
          <el-button v-if="scope.row.deleteMark===1" type="success" size="mini" @click="handleStatus(scope.row,0)">恢复</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"/>

    <!-- 添加或修改对话框 -->
    <!--    <el-dialog :visible.sync="dialogFormVisible" title="修改">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="dataForm"
        status-icon
        label-position="left"
        label-width="100px"
        style="width: 400px; margin-left:50px;">
        <el-form-item label="审核" prop="status">
          <el-select v-model="dataForm.status" clearable placeholder="请选择">
            <el-option :value="2" label="通过"/>
            <el-option :value="3" label="拒绝"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>-->

    <!-- 批量审核对话框 -->
    <el-dialog :visible.sync="dialogFormVisibleBatchPartner" title="选择审核类型" width="30%" >
      <el-form
        :rules="rules"
        :model="starLevelStatus"
        status-icon
        label-position="left"
        label-width="100px"
        style="width: 250px;  margin-left:50px;">
        <el-form-item label="通过/拒绝" prop="starLevelStatus" >
          <el-select v-model=" starLevelStatus" placeholder="请选择审核类型" >
            <el-option value="0" label="恢复正常" />
            <el-option value="1" label="隐蔽" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisibleBatchPartner = false">取消</el-button>
        <el-button size="mini" type="primary" @click="handleBatch">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .active{
    background-color: #1e6abc;
    color: white;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #20a0ff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
  }

  .avatar {
    width: 145px;
    height: 145px;
    display: block;
  }

  .input-new-equity {
    width: 300px;
    margin-left: 0px;
    vertical-align: bottom;
  }
</style>

<script>
import { listCourseComments, updateCourseComments, batchCourseComments } from '@/api/course'
// import {uploadPath} from '@/api/storage'
import { getToken } from '@/utils/auth'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'Partner',
  components: { Pagination },
  data() {
    return {
      // uploadPath,
      tags: [
        { name: '全部', cid: '' },
        { name: '1星', cid: 1 },
        { name: '2星', cid: 2 },
        { name: '3星', cid: 3 },
        { name: '4星', cid: 4 },
        { name: '5星', cid: 5 }
      ],
      bgindex: -1,
      updateParam: undefined,
      batchPartnerSelection: [],
      dialogFormVisibleBatchPartner: undefined,
      starLevelStatus: undefined,
      list: undefined,
      total: 0,
      listLoading: true,
      listQuery: {
        id: undefined,
        page: 1,
        limit: 20,
        courseId: undefined,
        userName: undefined,
        starLevel: undefined,
        sort: 'create_date',
        order: 'desc'
      },
      dataForm: {
        id: undefined,
        userId: undefined,
        status: undefined,
        addTime: undefined,
        updateTime: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      rules: {
        // name: [{ required: true, message: 'vip名称不能为空', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  computed: {
    headers() {
      return {
        'X-Litemall-Admin-Token': getToken()
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    // 标签类事件
    // 标签类事件
    tagclick(cid, index) {
      if (this.bgindex === index) {
        this.bgindex = -1
        this.listQuery.starLevel = ''
        this.getList()
      } else {
        this.bgindex = index
        this.listQuery.starLevel = cid
        this.getList()
      }
    },
    handleBatchPartnerChange(val) {
      this.batchPartnerSelection = val
    },
    batchPartnerUpdate() {
      if (this.batchPartnerSelection.length <= 0) {
        this.$confirm('当前没有批量审核内容，请先勾选左侧栏多选框！', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
      } else {
        this.dialogFormVisibleBatchPartner = true
      }
    },

    handleBatch() {
      if (this.starLevelStatus === '' || this.starLevelStatus === null || this.starLevelStatus === undefined || this.starLevelStatus === 0) {
        this.$confirm('请先选择消息审核类型', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        this.dialogFormVisibleBatchPartner = true
      } else {
        for (let i = 0; i < this.batchPartnerSelection.length; i++) {
          this.batchPartnerSelection[i].deleteMark = this.starLevelStatus
        }
        batchCourseComments(this.batchPartnerSelection)
          .then(() => {
            this.getList()
            this.dialogFormVisibleBatchPartner = false
            this.$notify.success({
              title: '成功',
              message: '更新数据成功'
            })
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.message
            })
          })
      }
    },
    init: function() {
      if (this.$route.query.courseId != null) {
        this.listQuery.courseId = this.$route.query.courseId
      }
      this.getList()
    },
    getList() {
      this.listLoading = true
      listCourseComments(this.listQuery)
        .then(response => {
          this.list = response.data.data.items
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updateCourseComments(this.dataForm)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.getList()
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新vip数据成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.message
              })
            })
        }
      })
    },
    handleStatus(parm, status) {
      this.updateParam = Object.assign({}, parm)
      this.updateParam.deleteMark = status
      let content = ''
      if (status === 0) {
        content = '是否恢复该评论？'
      } else if (status === 1) {
        content = '是否对其他用户隐藏该评论？'
      } else {
        content = '是否拒绝该用户的申请！！'
      }
      this.$confirm(content, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (status === 0) {
          updateCourseComments(this.updateParam)
            .then(() => {
              this.$notify.success({
                title: '成功',
                message: '已解封该评论'
              })
              // 刷新列表数据
              this.getList()
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.message
              })
              // 刷新列表数据
              this.getList()
            })
        } else if (status === 1) {
          updateCourseComments(this.updateParam)
            .then(() => {
              this.$notify.success({
                title: '成功',
                message: '已对其他用户隐藏该评论'
              })
              // 刷新列表数据
              this.getList()
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.message
              })
              // 刷新列表数据
              this.getList()
            })
        } else {
          updateCourseComments(this.updateParam)
            .then(() => {
              this.$notify.success({
                title: '成功',
                message: '审核已拒绝'
              })
              // 刷新列表数据
              this.getList()
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response.data.message
              })
              // 刷新列表数据
              this.getList()
            })
        }
      }).catch(() => {
        this.getList()
      })
    }
    // resetForm() {
    //   this.dataForm = {
    //     id: undefined,
    //     name: '',
    //     levelValue: undefined,
    //     scoreMultiple: 0.00,
    //     vipRule: '',
    //     equity: []
    //   }
    // },
    // handleCreate() {
    //   this.resetForm()
    //   this.dialogStatus = 'create'
    //   this.dialogFormVisible = true
    //   this.$nextTick(() => {
    //     this.$refs['dataForm'].clearValidate()
    //   })
    // },
    // createData() {
    //   this.$refs['dataForm'].validate(valid => {
    //     if (valid) {
    //       createVip(this.dataForm)
    //         .then(response => {
    //           this.list.unshift(response.data.data)
    //           // 更新L1目录
    //           this.getList()
    //           this.dialogFormVisible = false
    //           this.$notify.success({
    //             title: '成功',
    //             message: '创建成功'
    //           })
    //         })
    //         .catch(response => {
    //           this.$notify.error({
    //             title: '失败',
    //             message: response.data.message
    //           })
    //         })
    //     }
    //   })
    // }
    // handleDelete(row) {
    //   deleteVip(row)
    //     .then(response => {
    //       // 更新L1目录
    //       this.getList()
    //       this.$notify.success({
    //         title: '成功',
    //         message: '删除成功'
    //       })
    //       const index = this.list.indexOf(row)
    //       this.list.splice(index, 1)
    //     })
    //     .catch(response => {
    //       this.$notify.error({
    //         title: '失败',
    //         message: response.data.message
    //       })
    //     })
    // }
  }
}
</script>
